package com.kt.thread.demo;

import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReadWriteLock;

/**
 * Created by tangwy on 2016/1/14.
 */
public class CompletionServiceTest {
    public void poll(ExecutorService executor) {
        CompletionService completionService = new ExecutorCompletionService(executor);
        //创建10个任务
        for (int i = 0; i < 10; i++) {
            //任务编号
            final int taskID = i;
            completionService.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    Thread.sleep(100);
                    return taskID;
                }
            });
        }
        executor.shutdown();
        int counter = 0;
        while (counter < 10) {
            Future<Integer> future = completionService.poll();
            if (future == null) {
                System.out.println("暂无结果");
                try {
                    TimeUnit.MICROSECONDS.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    System.out.println(future.get());
                    counter++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public void take(ExecutorService executor) {
        CompletionService completionService = new ExecutorCompletionService(executor);
        //创建10个任务
        for (int i = 0; i < 10; i++) {
            //任务编号
            final int taskID = i;
            completionService.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    Thread.sleep(100);
                    return taskID;
                }
            });
        }
        executor.shutdown();
        int counter = 0;
        while (counter < 10) {
            Future<Integer> future = null;
            try {
                future = completionService.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //因为是take方法，所以会一直等到有任务完成返回，所以if(future == null)永远为false，if的代码也就
            //永远不会执行
            if (future == null) {
                System.out.println("暂无结果");
                try {
                    TimeUnit.MICROSECONDS.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    System.out.println(future.get());
                    counter++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
